﻿using Autodesk.AutoCAD.DatabaseServices;

namespace CADIfox_MVVM
{
    public static class Commandd
    {
        [CommandMethod(nameof(HelloWorld))]
        public static void HelloWorld()
        {
            using var tr = new DBTrans();
            Env.Editor.WriteMessage("hello world!");
            Env.Printl("开始画线：");
            Line line = new(new(0, 0, 0), new(1, 1, 0));
            tr.CurrentSpace.AddEntity(line);
            Env.Print("画线结束");
            Env.Editor.ZoomWindow(new(-1, -1, 0), new(2, 2, 0));
        }

        [CommandMethod("33", CommandFlags.UsePickSet)]
        public static void Apart()
        {
            using var tr = new DBTrans();

            var impliedSelect = tr.Editor.SelectImplied();
            if (impliedSelect.Status == PromptStatus.OK)
            {
                var seletedObjects = impliedSelect.Value.GetObjectIds();
                var layerids = new List<ObjectId>();
                foreach (var seletedObject in seletedObjects)
                {
                    var entity = tr.GetObject<Entity>(seletedObject);
                    layerids.Add(entity.LayerId);
                }
                var layers = tr.LayerTable.Except(layerids).ToList();
                foreach (var layer in layers)
                {
                    HideLyaer(tr, layer);
                }
            }
        }
        [CommandMethod("66", CommandFlags.UsePickSet)]
        public static void ShowAll()
        {
            using var tr = new DBTrans();

            var layerTbale = tr.LayerTable;
            layerTbale.ForEach(t =>
            {
                var layer = tr.GetObject<LayerTableRecord>(t, OpenMode.ForWrite);
                layer.IsOff = false;
            });
        }

        public static void ApartLayer(DBTrans tr, ObjectId objectId)
        {
            var layers = tr.LayerTable.Where(x => x != objectId);
            foreach (var layer in layers)
            {
                HideLyaer(tr, layer);
            }
        }

        public static void HideLyaer(DBTrans tr, ObjectId objectId)
        {
            var layer = tr.GetObject<LayerTableRecord>(objectId, OpenMode.ForWrite);
            layer.IsOff = true;
        }

    }
}